package no.ntnu.fp.messages.impl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.MessageCode;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.utils.XmlParser;

public class LogoutMessage implements Message {
	private final Employee employee;
	
	public LogoutMessage(){
		this(null);
	}
	
	public LogoutMessage(Employee employee){
		this.employee = employee;
	}
	
	public Employee getEmployee(){
		return this.employee;
	}
	
	@Override
	public String toString() {
		Document dom = XmlParser.getThreadLocalDocumentBuilder().newDocument();
		Element message = dom.createElement("message");
		message.appendChild(this.employee.toXml(dom));
		return getMessageCode() + "" + XmlParser.convertXmlToString(message);
	}

	@Override
	public MessageCode getMessageCode() {
		return MessageCode.LOGOUT_MESSAGE;
	}

	@Override
	public Message fromXmlString(String xml) {
		Document dom = XmlParser.parseXmlString(xml);
		return new LogoutMessage(new Employee(
				(Element)dom.getElementsByTagName("employee").item(0)));
	}

}
